home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / asm_subr.arc / SGND16IN < prev    next >
Encoding:
Text File  |  1985-12-22  |  2.1 KB  |  81 lines

  1. ;-------------------------sgnd16in routine begins--------------------------+
  2. ; from BLUEBOOK OF ASSEMBLY ROUTINES FOR IBM PC & XT.
  3. ;         page : 78
  4. ;
  5. ; NAME SGND16IN
  6. ; ROUTINE FOR Conversion From ASCII Signed Decimal To Binary
  7. ;
  8. ; FUNCTION: This routine accepts a signed decimal number from the std
  9. ; input device and converts it to internal signed two's complement 16-bit
  10. ; binary form.
  11. ;
  12. ; INPUT: Individual digits of the signed decimal number are received in
  13. ; ASCII through a call to a std input routine.  The sign (+/-) is optional
  14. ; and the valid digits are 0 through 9.  An ASCII code other than that for
  15. ; the sign and the valid digits terminates the routine.
  16. ;
  17. ; OUTPUT: A signed two's complement 16-bit binary number is returned in the
  18. ; DX register.
  19. ;
  20. ; REGISTERS USED:  AX and CX are modified.  DX is used for output.
  21. ; SEGMENTS REFERENCED:  None
  22. ; ROUTINES CALLED:  STDIN
  23. ; SPECIAL NOTES: None
  24. ;
  25. ; ROUTINE TO CONVERT FROM ASCII SIGNED DECIMAL TO INTERNAL TWO'S COMPLEMENT
  26. ; 16-BIT BINARY.
  27. ;
  28. sgnd16in    proc    far
  29. ;
  30.     mov    dx,0        ; initialize dx
  31.     mov    ch,0        ; sign flag
  32. ;
  33.     call    stdin        ; look for sign
  34.     cmp    al,'-'        ; was it minus ?
  35.     jz    sgnd16in1    ; yes... store it
  36.     cmp    al,'+'        ; was it a plus ?
  37.     jz    sgnd16in2    ; ignore pluses
  38.     jmp    sgnd16in3    ; anything else is a number
  39. ;
  40. sgnd16in1:
  41. ; set sign as a negative
  42.     mov    ch,0FFh        ; 0FFh is -1 in two's complement
  43. ;
  44. sgnd16in2
  45. ; normal loop
  46.     call    stdin        ; receive a digit in AL
  47. ;
  48. sgnd16in3:
  49. ; already have a digit
  50.     sub    al,30h        ; reduce from ASCII
  51.     jl    sgnd16in4        ; check if too low
  52.     cmp    al,9
  53.     jg    sgnd16in4    ; check if too high
  54.     cbw            ; convert to word
  55. ;
  56.     push    cx        ; save sign
  57.     push    ax        ; save digit
  58.     mov    ax,dx
  59.     mov    cx,10        ; multiplier of ten
  60.     mul    cx        ; multiply
  61.     mov    dx,ax        ; result in dx
  62.     pop    ax        ; restore digit
  63. ;
  64.     add    dx,ax        ; add in digit
  65.     pop    cx        ; restore count
  66.     jmp    sgnd16in2
  67. ;
  68. sgnd16in4:
  69. ; resolve the sign
  70.     cmp    ch,0        ; is it there ?
  71.     je    sgnd16in5    ; no... skip it
  72. ;
  73.     neg    dx        ; was negative
  74. ;
  75. sgnd16in5:
  76. ;
  77.     ret            ; return
  78. ;
  79. sgnd16in    endp
  80. ;-------------------------sgnd16in routine ends---------------------------+
  81.